import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "store";
import { ElMessage } from "element-plus";

// 动态导入（懒加载）
const LoginView = () => import('views/auth/Login.vue');
const RegisterView = () => import('views/auth/Register.vue');
const ForgotPasswordView = () => import('views/auth/ForgotPassword.vue');
const UserCenterView = () => import('views/user/UserCenter.vue');
const UserProfileView = () => import('views/user/UserProfile.vue');
const UserIdentityView = () => import('views/user/UserIdentity.vue');
const UserCompanyInfoView = () => import('views/user/UserCompanyInfo.vue');
const BankCardView = () => import('views/user/BankCard.vue');
const OtherDataView = () => import('views/user/OtherData.vue');
const FAQView = () => import('views/user/FAQ.vue');
const LoanHomeView = () => import('views/loan/LoanHome.vue');
const LoanListView = () => import('views/loan/LoanList.vue');
const LoanApplyView = () => import('views/loan/LoanApply.vue');
const LoanSuccessView = () => import('views/loan/LoanSuccess.vue');
const RepaymentPlanView = () => import('views/loan/RepaymentPlan.vue');
const RepaymentRecordView = () => import('views/loan/RepaymentRecord.vue');
const WalletView = () => import('views/loan/Wallet.vue');

// 路由配置
const routes = [
  {
    path: '/',
    name: 'home',
    component: LoanHomeView,
    meta: {
      title: '首页',
      requiresAuth: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: {
      title: '注册',
      requiresAuth: false
    }
  },
  {
    path: '/forgot-password',
    name: 'forgotPassword',
    component: ForgotPasswordView,
    meta: {
      title: '找回密码',
      requiresAuth: false
    }
  },
  {
    path: '/user',
    name: 'userCenter',
    component: UserCenterView,
    meta: {
      title: '个人中心',
      requiresAuth: true
    }
  },
  {
    path: '/user/profile',
    name: 'userProfile',
    component: UserProfileView,
    meta: {
      title: '个人资料',
      requiresAuth: true
    }
  },
  {
    path: '/user/identity',
    name: 'userIdentity',
    component: UserIdentityView,
    meta: {
      title: '身份信息',
      requiresAuth: true
    }
  },
  {
    path: '/user/bank-card',
    name: 'bankCard',
    component: BankCardView,
    meta: {
      title: '银行卡管理',
      requiresAuth: true
    }
  },
  {
    path: '/user/company-info',
    name: 'userCompanyInfo',
    component: UserCompanyInfoView,
    meta: {
      title: '公司信息',
      requiresAuth: true
    }
  },
  {
    path: '/user/additional',
    name: 'otherData',
    component: OtherDataView,
    meta: {
      title: '补充资料',
      requiresAuth: true
    }
  },
  {
    path: '/user/faq',
    name: 'faq',
    component: FAQView,
    meta: {
      title: '常见问题',
      requiresAuth: false
    }
  },
  {
    path: '/loan',
    name: 'loan',
    component: LoanListView,
    meta: {
      title: '我的贷款',
      requiresAuth: true
    }
  },
  {
    path: '/loan/apply',
    name: 'loanApply',
    component: LoanApplyView,
    meta: {
      title: '申请贷款',
      requiresAuth: true
    }
  },
  {
    path: '/loan/success',
    name: 'loanSuccess',
    component: LoanSuccessView,
    meta: {
      title: '申请成功',
      requiresAuth: true
    }
  },
  {
    path: '/loan/repayment-plan/:id',
    name: 'repaymentPlan',
    component: RepaymentPlanView,
    meta: {
      title: '还款计划',
      requiresAuth: true
    }
  },
  {
    path: '/loan/repayment-record',
    name: 'repaymentRecord',
    component: RepaymentRecordView,
    meta: {
      title: '还款记录',
      requiresAuth: true
    }
  },
  {
    path: '/wallet',
    name: 'wallet',
    component: WalletView,
    meta: {
      title: '我的钱包',
      requiresAuth: true
    }
  },
  // 404路由
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 贷款系统` : '贷款系统';
  
  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    const userStore = useUserStore();
    if (!userStore.isLoggedIn) {
      ElMessage.warning('请先登录');
      next({ name: 'login', query: { redirect: to.fullPath } });
      return;
    }
  }
  
  next();
});

export default router;